/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ServicesAccessData;

import DAO.Memcached;
import DAO.StatusDAO;
import DAO.TagDAO;
import DAO.UserDAO;
import POJO.StatusPOJO;
import POJO.TagNameAndCounter;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.smartcardio.CommandAPDU;
import javax.sql.CommonDataSource;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import profiler.*;

/**
 *
 * @author huy
 */
public class UserImpl implements User.Iface {

    private TTransport tr;
    private Cassandra.Client client;
    private  Timer timer;
    class RemindTask extends TimerTask {     
        @Override
        public void run() {      
            try {
                List<String> listMem = new ArrayList<String>();
                (StatusDAO.getInstance(client)).createCache(listMem);
                (TagDAO.getInstance(client)).createCacheTag(listMem);
                (UserDAO.getInstance(client)).createCache(listMem);
                Memcached.set("ListMemId", Common.Constants.EXPIRE, listMem);
                timer.cancel();      
                timer = new Timer();
                timer.schedule(new RemindTask(), Common.Constants.EXPIRE);
            } catch (UnavailableException ex) {
                Logger.getLogger(UserImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        } 
    }
    public UserImpl() throws InvalidRequestException, UnavailableException {
        try {
            tr = new TFramedTransport(new TSocket(Common.Constants.HOST, Common.Constants.PORT));
            TProtocol proto = new TBinaryProtocol(tr);
            client = new Cassandra.Client(proto);
            tr.open();
            client.set_keyspace(Common.Constants.KEYSPACE);

            //Khởi tạo cache.
            timer = new Timer();
            timer.schedule(new RemindTask(), Common.Constants.EXPIRE);

        } catch (TException ex) {
            Logger.getLogger(UserImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public boolean likeStatus(String statusID) throws TException {
        StatusDAO status = StatusDAO.getInstance(client);
        boolean result = status.likeStatus(statusID);
        return result;
    }

    @Override
    public boolean dislikeStatus(String statusID) throws TException {
        StatusDAO status = StatusDAO.getInstance(client);
        boolean result = status.dislikeStatus(statusID);
        return result;
    }

    @Override
    public List<StatusPOJO> selectStatusFromTag(String tagName) throws TException {
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectStatusFromTag(tagName);
        return result;
    }

    @Override
    public List<StatusPOJO> selectTopStatusFromTag(String tagName, int limit) throws TException {
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectTopStatusFromTag(tagName, limit);
        return result;
    }

    @Override
    public List<StatusPOJO> selectTopStatus(int limit) throws TException {
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectTopStatus(limit);
        return result;
    }

    @Override
    public List<StatusPOJO> selectRanDomStatus(int limit) throws TException {
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectRandomStatusV2(limit);
        return result;
    }

    @Override
    public List<StatusPOJO> selectRandomStatusFromTag(String tagName, int limit) throws TException {
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectRandomStatusFromTag(tagName, limit);
        return result;
    }

    @Override
    public List<TagNameAndCounter> selectAllTagNameAndCounter() throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        List<TagNameAndCounter> result = tag.selectAllTagNameAndCounter();
        return result;
    }

    @Override
    public List<TagNameAndCounter> selectTagNameAndCounter(int limit) throws TException {
        TagDAO tag = TagDAO.getInstance(client);
        List<TagNameAndCounter> result = tag.selectTagNameAndCounter(limit);
        return result;
    }

    @Override
    public boolean isBannedUser(String userID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        boolean result = user.isBannedUser(userID);
        return result;
    }

    @Override
    public List<StatusPOJO> selectFavoriteStatus(String userID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        List<StatusPOJO> result = user.selectFavoriteStatus(userID);
        return result;
    }

    @Override
    public boolean insertFavoriteStatus(String userID, String statusID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        boolean result = user.insertFavoriteStatus(userID, statusID);
        return result;
    }

    @Override
    public boolean removeFavoriteStatus(String userID, String statusID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        boolean result = user.removeFavoriteStatus(userID, statusID);
        return result;
    }

    @Override
    public boolean isAdminUser(String userID) throws TException {
        UserDAO user = UserDAO.getInstance(client);
        boolean result = user.isAdminUser(userID);
        return result;
    }

    @Override
    public boolean selectStatusFromTag_cache(String tagName) throws TException {
        boolean result = false;
        try {
            result = (StatusDAO.getInstance(client)).cachedStatusFromTag(tagName);
        } catch (UnavailableException ex) {
            Logger.getLogger(UserImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public boolean selectTopStatusFromTag_cache(String tagName) throws TException {
        boolean result = false;
        result = (StatusDAO.getInstance(client)).selectTopStatusFromTag_cache(tagName, 20);
        return result;
    }

    @Override
    public boolean selectTopStatus_cache() throws TException {
        boolean result = false;
        result = (StatusDAO.getInstance(client)).selectTopStatus_cache(20);
        return result;
    }

    @Override
    public boolean selectAllTagNameAndCounter_cache() throws TException {
        boolean result = false;
        result = (TagDAO.getInstance(client)).cachedAllTagNameAndCounter();
        return result;
    }

    @Override
    public boolean selectAllStatus_cache() throws TException {
        boolean result = false;
        result = (StatusDAO.getInstance(client)).selectAllStatus_cache();
        return result;
    }
}
